﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Xps.Packaging;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Interop.Excel;
using ProjectWizardManagementv1.ViewModels.Concrete;
using WordApplication = Microsoft.Office.Interop.Word.Application;
using PowerPointApplication = Microsoft.Office.Interop.PowerPoint.Application;
using ExcelApplication = Microsoft.Office.Interop.Excel.Application;


namespace ProjectWizardManagementv1.Infrastructure.Concrete
{
    public class OfficeFileViewer
    {
        public static Dictionary<string, Func<ProjectFileViewModel, string, string> > ConversionExtensionDictionary;

        static OfficeFileViewer()
        {
            ConversionExtensionDictionary = new Dictionary<string, Func<ProjectFileViewModel, string, string>>
                                        {
                                            {".txt", ViewWordDocument},
                                            {".docx",ViewWordDocument},
                                            {".doc", ViewWordDocument},
                                            {".pptx", ViewPowerPointDocument},
                                            {".ppt", ViewPowerPointDocument},
                                            {".xlsx", ViewExcelDocument},
                                            {".xls", ViewExcelDocument}
                                        };
        }

        public static XpsDocument ViewDocument(ProjectFileViewModel file, string tempSourcePath)
        {
            try
            {
                string viewName = ConversionExtensionDictionary[file.Extension].Invoke(file, tempSourcePath);
                var xpsDoc = new XpsDocument(viewName, FileAccess.Read);

                return xpsDoc;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.StackTrace);
            }
            return null;
        }

        public static string ViewWordDocument(ProjectFileViewModel file, string tempSourcePath)
        {
            var wordApp = new WordApplication();
            string viewName = string.Format("{0}~${1}_doc.xps",tempSourcePath, Path.GetFileNameWithoutExtension(file.Path));

            if(!File.Exists(viewName))
            {
                wordApp.Documents.Add(file.Path);
                Document doc = wordApp.ActiveDocument;
                
                doc.ExportAsFixedFormat(viewName, WdExportFormat.wdExportFormatXPS);
                wordApp.Quit( WdSaveOptions.wdDoNotSaveChanges, WdOriginalFormat.wdOriginalDocumentFormat);   
            }
            return viewName;
        }

        public static string ViewPowerPointDocument(ProjectFileViewModel file, string tempSourcePath)
        {
            var powerPointApp = new PowerPointApplication();
            string viewName = string.Format("{0}~${1}_ppt.xps", tempSourcePath, Path.GetFileNameWithoutExtension(file.Path));

            if(!File.Exists(viewName))
            {
                powerPointApp.Presentations.Open(file.Path, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
                powerPointApp.Presentations[powerPointApp.Presentations.Count].ExportAsFixedFormat(viewName, PpFixedFormatType.ppFixedFormatTypeXPS);

                powerPointApp.Quit();
            }
            return viewName;
        }

        public static string ViewExcelDocument(ProjectFileViewModel file, string tempSourcePath)
        {
            var excelApp = new ExcelApplication();

            string viewName = string.Format("{0}~${1}_xl.xps", tempSourcePath, Path.GetFileNameWithoutExtension(file.Path));

            if(!File.Exists(viewName))
            {
                excelApp.Workbooks.Add(file.Path);
                excelApp.ActiveWorkbook.ExportAsFixedFormat(XlFixedFormatType.xlTypeXPS, viewName);
                excelApp.Quit();
            }
            return viewName;
        }
    }
}
